package org.openfidias.jdbase.controller;

import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.openfidias.jdbase.locale.Message;
import org.openfidias.jdbase.model.Disciplina;
import org.openfidias.jdbase.model.DisciplinaDao;
import org.openfidias.jdbase.view.Console;

/**
 *
 * @author atila
 */
public class DisciplinaBo {

    private DisciplinaBo() {
    }

    public static DisciplinaBo getInstance() {
        return DisciplinaBoHolder.INSTANCE;
    }

    private static class DisciplinaBoHolder {
        private static final DisciplinaBo INSTANCE = new DisciplinaBo();
    }

    public void create() throws IOException {
       Disciplina disciplina = new Disciplina();
       System.out.println(StringUtils.center(" Criar Disciplina", 50, "·"));
       String value = "";

       // setting the codigo
       while (true) {
          System.out.println("Digite o Código: ");
          System.out.print(Console.bash);
          value = Console.read.nextLine();
          if (StringUtils.isNumeric(value)) {
             if (value.length() > Disciplina.maxCodigo) {
                System.out.println(Message.outOfBounds("Código", Disciplina.maxCodigo));
             } else if (DisciplinaDao.getInstance().exists(Integer.parseInt(value))) {
                System.out.println(Message.alreadyExists(DisciplinaDao.logicalTable, value));
             } else {
                disciplina.setCodigo(Integer.parseInt(value));
                break;
             }
          } else {
             System.out.println(Message.onlyNumeric("Código"));
          }
       }

       // setting nome
       while (true) {
          System.out.println("Digite o Nome: ");
          System.out.print(Console.bash);
          value = Console.read.nextLine();
          if (StringUtils.isAlphaSpace(value)) {
             if (value.length() > Disciplina.maxNome) {
                System.out.println(Message.outOfBounds("Nome", Disciplina.maxNome));
             } else {
                disciplina.setNome(value);
                break;
             }
          } else {
             disciplina.setNome(null);
          }
       }

       // setting curso
       while (true) {
          System.out.println("Digite o Curso: ");
          System.out.print(Console.bash);
          value = Console.read.nextLine();
          if (StringUtils.isAlphaSpace(value)) {
             if (value.length() > Disciplina.maxCurso) {
                System.out.println(Message.outOfBounds("Curso", Disciplina.maxCurso));
             } else {
                disciplina.setCurso(value);
                break;
             }
          } else {
             disciplina.setCurso(null);
          }
       }

       // saving to the database
       try {
          DisciplinaDao.getInstance().create(disciplina);
       } catch (Exception e) {
          System.out.println(Message.showError(e));
       }
    }

    public void read() {
       System.out.println(StringUtils.center(" Visualizar Disciplina ", 50, "·"));
       String value = "";
       System.out.println("Digite o Código: ");
       System.out.print(Console.bash);
       value = Console.read.nextLine();
       try {
          if (StringUtils.isNumeric(value)) {
             Disciplina disciplina = DisciplinaDao.getInstance().read(Integer.parseInt(value));
             System.out.println(Disciplina.headerTable());
             System.out.println(disciplina);
             System.out.println(Disciplina.footerTable());
          } else {
             System.out.println(Message.onlyNumeric("Código"));
          }
       } catch (Exception e) {
          System.out.println(Message.showError(e));
       }
    }

    public void delete() {
       System.out.println(StringUtils.center(" Deletar Disciplina ", 50, "·"));
       String value = "";
       System.out.println("Digite o Código: ");
       System.out.print(Console.bash);
       value = Console.read.nextLine();
       if (!( "".equals(value) ) && StringUtils.isNumeric(value)) {
          try {
             DisciplinaDao.getInstance().delete(Integer.parseInt(value));
          } catch (Exception e) {
             System.out.println(Message.showError(e));
          }
       } else {
          System.out.println(Message.onlyNumeric("Código"));
       }
    }
 }
